﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SqlServer.Dts.Runtime;
using System.IO;

namespace WolverineCustomActivities
{
	public partial class RunSSIS_Activity: Activity
	{
        public static DependencyProperty __ContextProperty = DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(RunSSIS_Activity));
        public static DependencyProperty PackagePathProperty = DependencyProperty.Register("PackagePath", typeof(System.String), typeof(RunSSIS_Activity));
        public static DependencyProperty ServerNameProperty = DependencyProperty.Register("ServerName", typeof(System.String), typeof(RunSSIS_Activity));
        public static DependencyProperty SQLUserProperty = DependencyProperty.Register("SQLUser", typeof(System.String), typeof(RunSSIS_Activity));
        public static DependencyProperty SQLPasswordProperty = DependencyProperty.Register("SQLPassword", typeof(System.String), typeof(RunSSIS_Activity));
        public static DependencyProperty OutputTextProperty = DependencyProperty.Register("OutputText", typeof(System.String), typeof(RunSSIS_Activity));
        public static DependencyProperty PackageVariablesProperty = DependencyProperty.Register("PackageVariables", typeof(System.String), typeof(RunSSIS_Activity));
        [Category("Wolverine Activities"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get
            {
                return (WorkflowContext)base.GetValue(RunSSIS_Activity.__ContextProperty);
            }
            set
            {
                base.SetValue(RunSSIS_Activity.__ContextProperty, value);
            }
        }

        [Category("Wolverine Activities"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string PackagePath
        {
            get
            {
                return (string)base.GetValue(RunSSIS_Activity.PackagePathProperty);
            }
            set
            {
                base.SetValue(RunSSIS_Activity.PackagePathProperty, value);
            }
        }
        [Category("Wolverine Activities"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ServerName
        {
            get
            {
                return (string)base.GetValue(RunSSIS_Activity.ServerNameProperty);
            }
            set
            {
                base.SetValue(RunSSIS_Activity.ServerNameProperty, value);
            }
        }
        [Category("Wolverine Activities"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string SQLUser
        {
            get
            {
                return (string)base.GetValue(RunSSIS_Activity.SQLUserProperty);
            }
            set
            {
                base.SetValue(RunSSIS_Activity.SQLUserProperty, value);
            }
        }
        [Category("Wolverine Activities"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string SQLPassword
        {
            get
            {
                return (string)base.GetValue(RunSSIS_Activity.SQLPasswordProperty);
            }
            set
            {
                base.SetValue(RunSSIS_Activity.SQLPasswordProperty, value);
            }
        }
        [Category("Wolverine Activities"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string OutputText
        {
            get
            {
                return (string)base.GetValue(RunSSIS_Activity.OutputTextProperty);
            }
            set
            {
                base.SetValue(RunSSIS_Activity.OutputTextProperty, value);
            }
        }
        [Category("Wolverine Activities"), Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string PackageVariables
        {
            get
            {
                return (string)base.GetValue(RunSSIS_Activity.PackageVariablesProperty);
            }
            set
            {
                base.SetValue(RunSSIS_Activity.PackageVariablesProperty, value);
            }
        }
		public RunSSIS_Activity()
		{
			InitializeComponent();
		}

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            string variables = null;
            if (this.PackageVariables != null)
            {
                Activity parent = executionContext.Activity;
                while (parent.Parent != null)
                {
                    parent = parent.Parent;
                }
                variables = Helper.ProcessStringField(this.PackageVariables, parent, null);
            }

            this.OutputText = RunPackageFromSQLServer(this.PackagePath, this.ServerName, this.SQLUser, this.SQLPassword,variables);
            return ActivityExecutionStatus.Closed;
        }

        static string RunPackageFromSQLServer(string PackagePath, string ServerName, string SQLUser, string SQLPassword, string PackageVariables)
        {
            try
            {
                Application SSIS = new Application();
                Package SSIS_Package;
                Variables SSIS_Variables;
                if (SQLUser != "")
                {
                    SSIS_Package = (Package)SSIS.LoadFromSqlServer(PackagePath, ServerName, SQLUser, SQLPassword, null);
                }
                else
                {
                    SSIS_Package = (Package)SSIS.LoadFromSqlServer(PackagePath, ServerName, null, null, null);
                }
                SSIS_Variables = SSIS_Package.Variables;
                foreach (string line in PackageVariables.Split('\r'))
                {
                    string[] pair = line.Split('=');
                    if (pair.Length == 2)
                    {
                        if (SSIS_Variables.Contains(pair[0]))
                        {
                            SSIS_Variables[pair[0]].Value = pair[1];
                        }
                    }
                }
                return SSIS_Package.Execute(null, SSIS_Variables, null, null, null).ToString(); ;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }


	}
}
